/*
 * RGImage.cpp
 *
 *  Created on: Jan 20, 2014
 *      Author: stepgalvao
 */

#include "RGImage.h"
#include "RGFeatureDetection.h"
#include <iostream>


RGImage::RGImage(Mat img) {
	sizeImg = img.size();
	this->img = img;
	ShowImage("1.jpg",this->img);
	this->imgBlur = BurnImage(this->img);
	ShowImage("2.jpg",this->imgBlur);
	this->imgThreshold = ThreSholdImage(this->imgBlur);
	ShowImage("3.jpg",this->imgThreshold);
	this->imgBorder = IdentifyBorder(this->imgThreshold);
	ShowImage("4.jpg",this->imgBorder);
}

Mat RGImage::GetImage() {
	return img;
}
Size RGImage::GetSize() {
	return sizeImg;
}

Mat RGImage::GetImageThreshold(){
	return imgThreshold;
}

Mat RGImage::GetImageBorder() {
	return imgBorder;
}

Mat RGImage::GetImageBlur() {
	return imgBlur;
}

Mat RGImage::GetImageWithBorder() {
	Mat dst;
	addWeighted(GetImage(), 0.5, GetImageBorder(), 0.5, 0, dst);
	return dst;
}


vector<KeyPoint> RGImage::GetFeatures() {

	if(features.empty()){
		RGFeatureDetection featureDetector(this->imgBorder);
		features = featureDetector.GetFeatures();
	}
	return features;
}

vector<Point2f> RGImage::GetFeaturesAsPoints() {
	vector<Point2f> points;
	for (int i = 0; i < GetFeatures().size(); i++)
		points.push_back(features[i].pt);
	return points;
}

Mat RGImage::ThreSholdImage(Mat img) {
	Mat tmp;
	threshold(img, tmp, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
	return tmp;
}

Mat RGImage::IdentifyBorder(Mat img) {
	Mat tmp;
	Laplacian(img, tmp, CV_8U);
	return tmp;
}

Mat RGImage::BurnImage(Mat img){
	Mat tmp;
	GaussianBlur(img, tmp, Size(3, 3),0);
	return tmp;
}

void RGImage::ShowImage(String nome, Mat img){
	if(SHOW_IMAGES)imshow(nome,img);
	if(CREATE_FILE)imwrite(nome,img);
}


